/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.CursoData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Max
 */
public class ActualizarCurso extends javax.swing.JInternalFrame {

    /**
     * Creates new form ActualizarCurso
     */
    CursoData curData = new CursoData();
    CarreraData caData = new CarreraData();

    public ActualizarCurso() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jComboCarreras.removeAllItems();
        cargarListaCarreras();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTbxNombre = new javax.swing.JTextField();
        jTbxCreditos = new javax.swing.JTextField();
        jTbxHoras = new javax.swing.JTextField();
        jBtnAceptar = new javax.swing.JButton();
        jBtnCancelar = new javax.swing.JButton();
        jComboCarreras = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListCursos = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTbxSigla = new javax.swing.JTextField();
        jBtnEditar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTbxProfesor = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Actualizar curso");

        jLabel1.setText("Carrera:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Créditos");

        jLabel4.setText("Horas semanales:");

        jTbxNombre.setEnabled(false);

        jTbxCreditos.setEnabled(false);

        jTbxHoras.setEnabled(false);

        jBtnAceptar.setText("Aceptar");
        jBtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAceptarActionPerformed(evt);
            }
        });

        jBtnCancelar.setText("Cancelar");

        jComboCarreras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboCarrerasActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jListCursos);

        jLabel6.setText("Cursos:");

        jLabel5.setText("Sigla:");

        jTbxSigla.setEnabled(false);

        jBtnEditar.setText("Editar");
        jBtnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEditarActionPerformed(evt);
            }
        });

        jLabel7.setText("Profesor:");

        jTbxProfesor.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnCancelar))
                            .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBtnEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(49, 49, 49)
                                .addComponent(jTbxCreditos))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(48, 48, 48))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(63, 63, 63)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTbxNombre)
                                    .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addGap(44, 44, 44)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTbxHoras)
                                    .addComponent(jTbxProfesor))))
                        .addGap(36, 36, 36))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTbxCreditos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTbxHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(48, 48, 48)
                        .addComponent(jBtnEditar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTbxProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBtnCancelar)
                            .addComponent(jBtnAceptar))
                        .addGap(48, 48, 48))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }
    }

    private void cargarListaCursos(String codCarrera) throws SQLException, ExcepcionGlobal, NoDataException {
        DefaultListModel modelo = new DefaultListModel();
        jListCursos.setEnabled(true);
        ArrayList<Curso> cursos = new ArrayList<Curso>();
        cursos = curData.getCursosPorCarrera(codCarrera);
        if (cursos.isEmpty()) {
            JOptionPane.showMessageDialog(null, "No hay datos asociados a la carrera");
            jListCursos.setEnabled(false); 
        } else {
            for (int i = 0; i < cursos.size(); i++) {
                //aca puedes poner la validacion de tus datos antes de agregarlo al modelo
                modelo.addElement(cursos.get(i));
            }
            this.jListCursos.setModel(modelo);
        }

    }
    private void jBtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAceptarActionPerformed
        String sigla, nombre = null;
        int creditos, horasSemanales = 0;
        sigla = jTbxSigla.getText().toString().toUpperCase();
        nombre = jTbxNombre.getText();
        creditos = Integer.parseInt(jTbxCreditos.getText());
        horasSemanales = Integer.parseInt(jTbxCreditos.getText());
        try {
            curData.actualizarCurso(sigla, nombre, creditos, horasSemanales);
            JOptionPane.showMessageDialog(null, "Éxito en la ejecución");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocurrió un fallo durante la ejecución");
        }
        this.dispose();
    }//GEN-LAST:event_jBtnAceptarActionPerformed
    private void eliminarDatosLista(DefaultListModel model) {
        for (int i = 0; i < model.size(); i++) {
            model.remove(i);
        }
    }
    private void jComboCarrerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboCarrerasActionPerformed
        Carrera carrSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
        String codCarrera = carrSeleccionada.getCodigo();
        try {
            cargarListaCursos(codCarrera);
        } catch (Exception e) {
        }

    }//GEN-LAST:event_jComboCarrerasActionPerformed

    private void jBtnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEditarActionPerformed
        Curso cursoSeleccionado, cursoRetornado = null;
        cursoSeleccionado = (Curso) jListCursos.getSelectedValue();
        try {
            cursoRetornado = curData.getCursoPorSigla(cursoSeleccionado.getSigla());
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(ActualizarCurso.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(ActualizarCurso.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTbxSigla.setText(cursoRetornado.getSigla());
        jTbxNombre.setText(cursoRetornado.getNombre());
        jTbxCreditos.setText(String.valueOf(cursoRetornado.getCreditos()));
        jTbxHoras.setText(String.valueOf(cursoRetornado.getHoras()));
        jTbxProfesor.setText(cursoRetornado.getProfesor().getNombre());
        jTbxNombre.enable(true);
        jTbxCreditos.enable(true);
        jTbxHoras.enable(true);
    }//GEN-LAST:event_jBtnEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAceptar;
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JButton jBtnEditar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jListCursos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTbxCreditos;
    private javax.swing.JTextField jTbxHoras;
    private javax.swing.JTextField jTbxNombre;
    private javax.swing.JTextField jTbxProfesor;
    private javax.swing.JTextField jTbxSigla;
    // End of variables declaration//GEN-END:variables
}
